package com.clickpaas.timelineofUN.dao.Impl;

import com.bizcloud.ipaas.t7aef1c33e4a445f2833f9d631cb90c38.d20210202091529.codegen.ProjectApi;
import com.bizcloud.ipaas.t7aef1c33e4a445f2833f9d631cb90c38.d20210202091529.model.*;
import com.clickpaas.timelineofUN.bean.TimelineDTOBean;
import com.clickpaas.timelineofUN.dao.ProjectDao;
import com.clickpaas.timelineofUN.entity.ProjectEntity.ProjectEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
@Slf4j
public class ProjectDaoImpl implements ProjectDao {

    @Resource
    private TimelineDTOBean timelineDtoBean;
    @Resource
    private ProjectApi projectApi;

    /**
     * 根据ProejctProviderId查询项目数据
     *
     * @param ProejctProviderId
     * @return
     */
    @Override
    public List<ProjectDTOResponse> projectByProejct(String ProejctProviderId) {
        ProjectDTO projectDTO = timelineDtoBean.projectDTO();
        projectDTO.setProejctProvider(ProejctProviderId);
        return queryProject(projectDTO);
    }

    /**
     * 根据ProejctProviderId、projectName查询项目数据
     *
     * @param ProejctProviderId
     * @param projectName
     * @return
     */
    @Override
    public List<ProjectDTOResponse> projectByProejct(String ProejctProviderId, String projectName) {
        ProjectDTO projectDTO = timelineDtoBean.projectDTO();
        projectDTO.setProejctProvider(ProejctProviderId);
        projectDTO.setProjectName(projectName);
        return queryProject(projectDTO);
    }

    /**
     * 查询项目数据
     *
     * @param entity
     * @return
     */
    @Override
    public List<ProjectDTOResponse> queryProject(ProjectEntity entity) {
        ProjectDTO projectDTO = timelineDtoBean.projectDTO();
        BeanUtils.copyProperties(entity,projectDTO);
        return queryProject(projectDTO);
    }

    private List<ProjectDTOResponse> queryProject(ProjectDTO projectDTO){
        DataResponseListProjectDTO projectUsingInfo = projectApi.findProjectUsingPOST(projectDTO);
        if (null == projectUsingInfo
                || null == projectUsingInfo.getData()
                || !"SUCCESS".equals(projectUsingInfo.getMessage())){
            log.info("当前查询的数据：【{}】",projectUsingInfo);
            return null;
        }
        return projectUsingInfo.getData();
    }
}
